#-*- coding:utf-8 -*-

from flask import render_template, request, session, flash, jsonify

from . import main
import user_model
from ..model import User
from .. import tools

@main.route('/')
@main.route('/login', methods=['GET', 'POST'])
def login():
    error = None
    msg = None
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        error = user_model.check_user(username, password)
        if error == 0:
            session['username'] = username
            flash('You were successfully logged in')
            return render_template('examindex.html')
        else:
            print error
            error_msg = tools.get_error_msg(error)
            return render_template('login.html', error=error, msg=error_msg)
    elif request.method == 'GET':
        return render_template('login.html')

@main.route('/logout')
def logout():
    session.pop['username']
    return render_template('login.html')

@main.route('/regis', methods=['GET', 'POST'])
def regis():
    error = None
    msg = None
    if request.method == 'GET':
        return render_template('regis.html')
    elif request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        dspnam = request.form['dspnam']
        user = User(None, username, password, dspnam=dspnam)
        error = user_model.add_user(user)
        return jsonify({"errorCode":error})

@main.route('/examindex', methods=['GET', 'POST'])
def examindex():
    """ 考试欢迎界面，根据考生情况显示初始欢迎界面和进阶考试界面 """
    if request.method == 'GET':
        pass